package com.creatingrevolutions.vbukit.lib;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created by warodri on 7/21/13.
 */
public class DataActivity
{
	Exception lastError = null;
	
	public String executePOST(final String url,
			List<NameValuePair> nameValuePairs)
	{
		StringBuilder builder = new StringBuilder();
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		
		try
		{
			// Add your data
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200)
			{
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null)
				{
					builder.append(line);
				}
			}
			else
			{
				Log.e(DataActivity.class.toString(), "Failed to download file");
			}
			
			lastError = null;
			return builder.toString();
			
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
			lastError = e;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			lastError = e;
		}
		
		return null;
	}
	
	public String executeGET(final String url)
	{
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try
		{
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200)
			{
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null)
				{
					builder.append(line);
				}
			}
			else
			{
				Log.e(DataActivity.class.toString(), "Failed to download file");
			}
			
			lastError = null;
			return builder.toString();
			
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
			lastError = e;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			lastError = e;
		}
		
		return null;
		
	}
	
	public Exception getLastError()
	{
		return this.lastError;
	}
}